package com.ddl.controller;

import com.ddl.common.exception.ExceptionUtil;
import com.ddl.common.exception.ResultModel;
import com.ddl.pojo.User;
import com.ddl.service.SmsCodeService;
import com.ddl.service.UserService;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2018-11-01
 */
@RestController
@RequestMapping(value = "/app/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @Autowired
    SmsCodeService smsCodeService;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/register")
    public ResultModel register(@RequestBody User user) {
        try {
            return userService.register(user);
        } catch (Exception e) {
            logger.error("register fail, user:{}", new Gson().toJson(user), e);
            return ExceptionUtil.getMessageException(e);
        }
    }

    /**
     * 用户登陆
     *
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/login")
    public ResultModel login(@RequestBody Map<String, Object> map) {
        try {
            String account = map.get("account").toString();
            String password = map.get("password").toString();
            return userService.login(account, password);
        } catch (Exception e) {
            logger.error("login fail, map:{}", new Gson().toJson(map), e);
            return ExceptionUtil.getMessageException(e);
        }
    }

    /**
     * 找回账号
     *
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findAccount")
    public ResultModel findAccount(@RequestBody Map<String, Object> map) {
        try {
            String phone = map.get("phone").toString();
            String smsCode = map.get("smsCode").toString();
            String password = map.get("password").toString();
            return userService.findAccount(phone, smsCode, password);
        } catch (Exception e) {
            logger.error("findAccount fail, map:{}", new Gson().toJson(map), e);
            return ExceptionUtil.getMessageException(e);
        }
    }
    /**
     * 是否首次登陆
     *
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/isFirstLogin")
    public ResultModel isFirstCome(HttpServletRequest request) {
        try {
            String userId = request.getHeader("userId");
            logger.info("是否首次登陆接口userId={}",userId);
            return userService.isFirstLogin(userId);
        } catch (Exception e) {
            logger.error("是否首次登陆 fail:{}", e);
            return ExceptionUtil.getMessageException(e);
        }
    }
}
